package com.itheima11;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author Xiao
 * @date 2021/7/27
 */
public class StudentTest {
    /**
     * TreeMap练习：
     *      创建一个TreeMap集合，键是学生对象（Student），
     *      值是籍贯（String），学生属性姓名和年龄，按照年龄进行排序并遍历。
     */
    public static void main(String[] args) {
        Student stu1 = new Student("aliang", 24);
        Student stu2 = new Student("zuoyou", 25);
        Student stu3 = new Student("pingan", 21);
        Student stu4 = new Student("dafeng", 28);
        Student stu5 = new Student("baoping", 16);
        TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o2.getAge() - o1.getAge();
                return result == 0 ? o1.getName().compareTo(o2.getName()) : result;
            }
        });
        tm.put(stu1, "湖南");
        tm.put(stu2, "广州");
        tm.put(stu3, "上海");
        tm.put(stu4, "天津");
        tm.put(stu5, "湖北");
        Set<Map.Entry<Student, String>> entries = tm.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            System.out.println(entry.getKey() + "\t" +entry.getValue());
        }
        System.out.println("----------");
        tm.forEach((Student stu, String s) -> {
            System.out.println(stu + "\t" + s);
        });
        System.out.println("----------");
        for (Student stu : tm.keySet()) {
            System.out.println(stu + "\t" + tm.get(stu));
        }
    }
}
